/** \file ezlcdtest.c
 * Source code for "the squares" part of the demo for the ezlcd3xx Arduino 
 * library.
 */

/*
 * Written and maintained by Sergey Butylkov. 
 * Created on behalf of EarthLCD.com a dba of Earth Computer Technologies Inc.
 *
 * Project home: http://arduino-ezlcd3xx.googlecode.com
 *
 * Copyright (C) 2012 Sergey Butylkov
 * Copyright (C) 2012 EarthLCD.com a dba of Earth Computer Technologies, Inc.
 * 
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version. This work is distributed in the hope that it will be 
 * useful, but without any warranty; without even the implied warranty of 
 * merchantability or fitness for a particular purpose. See the GNU Lesser 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include "ezlcd3xx.h"

int width;
int height;

int xpos = 100, ypos = 100;
int xvel = 3, yvel = 3;
int boxwidth = 50;
int background = 0;
int foreground = 1;
unsigned int count = 0;

void demo_setup( EzLCD3xx &lcd )
{
    int i;
    width = lcd.xmax()+1;
    height = lcd.ymax()+1;
    lcd.cls( background );
    lcd.string( 1, "ezLCD301" );
    lcd.font( "Squine48" );
}

void demo_loop( EzLCD3xx &lcd )
{
    xpos += xvel;
    ypos += yvel;
    if( xpos < 0 )
    {
        xpos = 0;
        xvel = -xvel;
    }
    else if( xpos >= width-boxwidth )
    {
        xpos = width-boxwidth-1;
        xvel = -xvel;
    }

    if( ypos < 0 )
    {
        ypos = 0;
        yvel = -yvel;
    }
    else if( ypos >= height-boxwidth )
    {
        ypos = height-boxwidth-1;
        yvel = -yvel;
    }

    foreground = (foreground + 1)%16;
    lcd.color( foreground );
    lcd.xy( xpos, ypos );
    lcd.box( boxwidth, boxwidth, true );

    if( count++ % 1000 == 0 )
    {
        lcd.cls( background );
        lcd.color( 1 + random(15) );
        for( int i = 0; i < 10; ++i )
        {
            lcd.xy( random(width), random(height) );
            lcd.print( 1, CENTER );
        }
    }
}